import { FrameworkConfiguration, LoadableApp, loadMicroApp, ObjectType } from "qiankun";

const uuid = Math.random().toString(36).split('.')[1].toLocaleUpperCase();
let ascendingId = 0;

/**获取页面的数量 */
export function getAscendingId(): string {
  return uuid + '_' + ascendingId++;
}

/**获取布尔类型 */
export function getBoolean(value): boolean {
  if (value === 'true') {
    return true;
  }
  if (value === 'false') {
    return false;
  }
  if (isNaN(Number(value))) {
    return true;
  }
  return Boolean(value);
}


// 队列形式加载
const queueMap: { [key: string]: any[] } = {};
/**
 * @param {string} url
 * @param {{ opt: any; config: any }} params
 * @param {*} cb 回调函数
 * @param {boolean} [isQueue]  是否队列形式加载
 * @param {boolean} [isRun] 是否立即运行
 */
export function loadQueueApp(url: string, params: { opt: LoadableApp<ObjectType>; config: FrameworkConfiguration }, cb, isQueue?: boolean, isRun?: boolean) {
  if (!queueMap[url]) {
    queueMap[url] = [];
  }

  if (!isRun && isQueue) {
    if (queueMap[url].length > 0) {
      queueMap[url].push({ params, cb });
      return;
    }
    queueMap[url].push({ params, cb });
  }
  const micApp = loadMicroApp(params.opt, params.config);

  micApp.mountPromise.then(() => {
    queueMap[url].shift();
    if (queueMap[url].length > 0) {
      const loadApp = queueMap[url][0];
      loadQueueApp(url, loadApp.params, loadApp.cb, isQueue, true);
    }
    cb(micApp);
  });
}


